

import axios from "axios";
import * as actionTypes from './actionType'

export const addCounter=(counter)=>{
    return {
        type:actionTypes.ADDCOUNTER,
        counter
    }
}

export const subCounter =(counter)=>{
    return {
        type:actionTypes.SUBCOUNTER,
        counter
    }
}


export const addBanner=(banner)=>{
    return {
        type:actionTypes.ADDBANNER,
        banner
    }
}
export const addProductList=(productList)=>{
    return {
        type:actionTypes.ADDPRODUCTLIST,
        productList
    }
}



export const fetchHomeMultidataAction=()=>{
   /*  // 借用thunk 中间件 派发一个函数
    // 两个参数 dispatch,getState
    function foo(dispatch,getState){
      // console.log('foo');
      // console.log('dispatch');
      // console.log('getState',getState().counter);
      axios.get('http://123.207.32.32:8000/home/multidata').then(res => {
          const banners = res.data.data.banner.list;
          const recommends= res.data.data.recommend.list;
          dispatch(addBanner(banners));
          dispatch(addProductList(recommends));
      })
    }
    return foo; */

    return function (dispatch,getState) {
      axios.get('http://123.207.32.32:8000/home/multidata').then(res => {
        const banners = res.data.data.banner.list;
        const recommends = res.data.data.recommend.list;
        dispatch(addBanner(banners));
        dispatch(addProductList(recommends));
      })
    }
}